package com.aaa.fpa.controller;

import cn.hutool.core.util.ObjectUtil;
import com.aaa.fpa.service.*;
import com.aaa.pojo.*;
import com.aaa.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/front")

public class FrontController extends BaseController  {
    @Autowired
    private ViewTestPlanService viewTestPlanService;
    @Autowired
    private TesterFpaService testerFpaService;
    @Autowired
    private QuestionFpaService questionFpaService;
    @Autowired
    private QuestionSasService questionSasService;
    @Autowired
    private TblQuestionSdsService tblQuestionSdsService;

    @Autowired
    private TestResultFpaService testResultFpaService;
    @Autowired
    private TestResultSasService testResultSasService;
    @Autowired
    private TestResultSdsService testResultSdsService;


    MyReturn myReturn= new MyReturn();
    @RequestMapping("/checkTest.do")
    @ResponseBody
    public MyReturn checkTest(@RequestBody TesterVo tester, HttpSession httpSession) {
        ViewTestPlan testPlan = this.viewTestPlanService.queryByTestCode(tester.getTestCode());
        if (ObjectUtil.isNotEmpty(testPlan)) {
            Date date = new Date();
            if (date.after(testPlan.getTestBegin()) && date.before(testPlan.getTestEnd())) {
                boolean flag = false;
                switch (testPlan.getType()) {
                    case "fpa":
                        List<Map<String, Object>> byPhone = this.testerFpaService.findTesterByPhone(tester.getPhone());
                        if (ObjectUtil.isNotEmpty(byPhone) && byPhone.size() > 0) {
                            flag = true;
                        }
                        break;
                    case "sas":
                        List<Map<String, Object>> byPhone1 = this.testerFpaService.findSasTesterByPhone(tester.getPhone());
                        if (ObjectUtil.isNotEmpty(byPhone1) && byPhone1.size() > 0) {
                            flag = true;
                        }
                        break;
                    case "sds":
                        List<Map<String, Object>> byPhone2 = this.testerFpaService.findSdsTesterByPhone(tester.getPhone());
                        if (ObjectUtil.isNotEmpty(byPhone2) && byPhone2.size() > 0) {
                            flag = true;
                        }
                }

                if (flag) {
                    this.myReturn.setCode(1);
                    this.myReturn.setMsg("对不起，您的手机号已经做过测试了！");
                } else {
                    this.myReturn.setCode(0);
                    tester.setTestPlanId(testPlan.getId());
                    tester.setType(testPlan.getType());
                    tester.setTestName(testPlan.getTestName());
                    httpSession.setAttribute("tester", tester);
                }
            } else {
                this.myReturn.setCode(1);
                this.myReturn.setMsg("对不起，测试码已失效");
            }
        } else {
            this.myReturn.setCode(1);
            this.myReturn.setMsg("对不起，邀请码无效，请确认！");
        }

        return this.myReturn;
    }

    @RequestMapping("/findQuestion.do")
    public ModelAndView findQuestion(HttpSession httpSession, ModelAndView modelAndView){
        //获取session中保存的测试者信息
        TesterVo tester = (TesterVo) httpSession.getAttribute("tester");
        switch (tester.getType()){
            case Constants.FPA:
                //查询所有可以测试的fpa测试题
                List<QuestionFpa> questionFpaList = questionFpaService.findAllQuestion();
                modelAndView.addObject("questionList",questionFpaList);
                break;
            case Constants.SAS:
                //查询所有可以测试的sas测试题
                List<QuestionSas> questionSasList = questionSasService.findAllQuestion();
                modelAndView.addObject("questionList",questionSasList);
                break;
            case Constants.SDS:
                //查询所有可以测试的sds测试题
                List<TblQuestionSds> questionSdsList = tblQuestionSdsService.findAllQuestion();
                modelAndView.addObject("questionList",questionSdsList);
                break;
            default:break;

        }
        modelAndView.setViewName("/front/testQuestion.jsp");
        return modelAndView;
    }
    @RequestMapping("/saveBatchTestResult.do")
    @ResponseBody
    public MyReturn saveBatchTestResult(@RequestBody List<Map<String,Object>> answerList,HttpSession httpSession){

        //获取session中保存的测试者信息
        TesterVo tester = (TesterVo) httpSession.getAttribute("tester");

        if (tester.getType().equals(Constants.FPA)) {
            String testResult = testResultFpaService.saveTestResult(answerList, tester);
            myReturn.setData(testResult);
            return myReturn;
        }else if (tester.getType().equals(Constants.SAS)){
            String testResult = testResultSasService.saveTestResult(answerList, tester);
            myReturn.setData(testResult);
            return myReturn;
        } else if (tester.getType().equals(Constants.SDS)){
            String testResult = testResultSdsService.saveTestResult(answerList, tester);
            myReturn.setData(testResult);
            return myReturn;
        }
        return null;
    }

}